#!/usr/bin/env bash

#
# Copyright (C) 2021 The Delta Lake Project Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

PYTHON_EXECUTABLE="${PYTHON_EXECUTABLE:-python3}"

set -o pipefail
set -e

if ! hash pytest 2> /dev/null; then
    echo "The pytest command was not found. Please install 'pytest' Python package."
    exit 1
fi

# The current directory of the script.
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

FWDIR="$( cd "$DIR"/.. && pwd )"
cd "$FWDIR"

if [ -n "AWS_ACCESS_KEY_ID" ]; then
    logopts=(-o log_cli=true -s)
fi

# Runs both doctests and unit tests by default, otherwise hands arguments over to pytest.
if [ "$#" = 0 ]; then
    # delta_sharing/_yarl_patch.py is a hack to support GCS pre-signed urls. Ask pytest to not
    # import it automatically so that we can verify we are importing it on demand.
    $PYTHON_EXECUTABLE -m pytest --ignore=delta_sharing/_yarl_patch.py --verbose --showlocals --color=yes --doctest-modules delta_sharing "${logopts[@]}"
else
    $PYTHON_EXECUTABLE -m pytest "$@"
fi
